<?php
class gather_mod extends admin_mod {

    public function addAct(){
        $id   = intval($this->get('id'));
        $step = intval($this->get('step'));
        $this->assign('id'   , $id);
        $this->assign('step' , $step);
        $ga = $GLOBALS['_CACHE']->get('ga-'.$id , '');
        if($step == 1 ){
            $ga = $ga ? array_merge($ga ,  $this->get('gather' , 'P')) : $this->get('gather' , 'P');
            $GLOBALS['_CACHE']->set('ga-'.$id , $ga , '');
            if(!isset(config_lib::$gather[$step])){
                $this->showmsg(" {$ga['name']} 规则配置完毕 ..." , core::url('admin@gather-test-id-%d' , gather_lib::add($ga)) , 1 , 2);
            }
        }else if($id === 0){
            // 初始化
            $GLOBALS['_CACHE']->clear('ga-'.$id , '');
        }
        $this->assign('ga' , $ga);
        $this->assign('atitle' , '新增采集');
        $this->assign('config' , config_lib::$gather[$step]);
        $this->display('gather/add.html');
    }

    /**
     * 采集测试配置
     *
     */
    public function testAct(){
        $id = intval($this->get('id'));
        $ga = gather_lib::get($id);
        if(empty($ga)){
            $this->showmsg('暂无待测试采集!');
        }
        $this->assign('ga' , $ga);
        $this->assign('atitle' , '采集测试');
        $this->assign('config' , config_lib::$gather);
        $this->assign('id' , $id);
        $this->display('gather/test.html');
    }

    public function debugAct(){
        $this->nocache();
        $r  = $this->get('r');
        $k  = intval($this->get('k'));
        $id = intval($this->get('id'));
        $ga = gather_lib::get($id);
        $this->assign('kv' , $k);
        $this->assign('id' , $id);
        if(isset($ga[$r])){
            $this->ajaxout(gather_lib::debug($ga , $id , $k , $r));
        }else{
            $this->assign('config' , config_lib::$gather[$k]);
            $this->assign('ga' , $ga);
            $this->display('gather/debug.html');
        }
        $this->ajaxout('未指定的操作');
    }

    public function upAct(){
        $id = $insid = intval($this->get('id')) | intval($this->get('gather:id' , 'P'));
        $ga = gather_lib::get($id);
        $v  = $this->get('gather' , 'P');
        if(!empty($v)){
            $v['id'] = $ga ? $ga['id'] : 0;
            $insid = gather_lib::add(!empty($ga) ? array_merge($ga , $v) : $v);
        }
        $this->ajaxout(intval($id > 0 ? $id : $insid) , 'text');
    }
}
?>